package com.wlk.controller;

import com.wlk.Result.Result;
import com.wlk.service.UsersService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;

@Controller
public class LoginController {
    @Resource
    private UsersService usersService;

    @GetMapping("/login")
    public String showLoginPage() {
        return "login";
    }

    @PostMapping("/login")
    public String handleLogin(@RequestParam("phone") String phone,
                              @RequestParam("password") String password,
                              Model model) {
        // 在这里添加你的登录逻辑
        Result result = usersService.getUserByPhoneAndPassword(phone, password);
        if (result.getCode() == 200) {
            // 登录成功
            model.addAttribute("user", result.getData());
            return "redirect:/index";
        } else {
            // 登录失败
            model.addAttribute("error", "用户名或密码错误");
            return "login";

        }
    }
}

